#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Git Hooks 动态配置系统 - 简化演示脚本
"""

import sys
import os
from pathlib import Path

# 添加项目根目录到 Python 路径
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))

from core.hook_manager import HookManager, HookType, HookContext

def main():
    """主演示函数"""
    print("Git Hooks Dynamic Configuration System - Demo")
    print("=" * 50)
    
    # 确保使用正确的配置目录路径
    config_dir = project_root / "config"
    plugins_dir = project_root / "plugins"
    manager = HookManager(str(config_dir), str(plugins_dir))
    
    # 1. 测试无效提交信息（缺少 Issue 和 Signed-off-by）
    print("\n1. Testing INVALID commit message (missing Issue and Signed-off-by):")
    print("   Message: 'feat: add new feature'")
    
    context = HookContext(hook_type=HookType.COMMIT_MSG)
    context.commit_msg = 'feat: add new feature'
    success = manager.execute_hook(HookType.COMMIT_MSG, context)
    
    print(f"   Result: {'PASSED' if success else 'REJECTED (Expected)'}")
    
    # 2. 测试有效提交信息（包含 Issue 和 Signed-off-by）
    print("\n2. Testing VALID commit message (with Issue and Signed-off-by):")
    valid_msg = """feat: add new authentication feature

Issue: #123
This feature adds OAuth 2.0 authentication support.

Signed-off-by: John Doe <john.doe@huawei.com>"""
    print("   Message: (Multi-line message with Issue and Signed-off-by)")
    
    context = HookContext(hook_type=HookType.COMMIT_MSG)
    context.commit_msg = valid_msg
    success = manager.execute_hook(HookType.COMMIT_MSG, context)
    
    print(f"   Result: {'PASSED' if success else 'REJECTED'}")
    
    # 3. 测试pre-commit Hook
    print("\n3. Testing pre-commit hook:")
    context = HookContext(hook_type=HookType.PRE_COMMIT)
    success = manager.execute_hook(HookType.PRE_COMMIT, context)
    
    print(f"   Result: {'PASSED' if success else 'FAILED'}")
    
    # 4. 显示支持的格式
    print("\n4. Required commit message format:")
    print("   Format: <type>: <description>")
    print("   ")
    print("   Required elements:")
    print("   1. Type: feat|fix|docs|style|refactor|test|chore")
    print("   2. Issue reference: Issue: #123 or Fixes #123 or #123")
    print("   3. Signed-off-by: Name <email@allowed-domain.com>")
    print("   ")
    print("   Allowed email domains:")
    print("   - h-partners.com")
    print("   - huawei-partners.com") 
    print("   - huawei.com")
    print("   ")
    print("   Example:")
    example = """   feat: add user authentication
   
   Issue: #123
   Implemented OAuth 2.0 authentication system.
   
   Signed-off-by: John Doe <john.doe@huawei.com>"""
    print(example)
    
    print("\n" + "=" * 50)
    print("Demo completed!")
    print("\nNext steps:")
    print("1. Edit config/hooks.yaml to customize")
    print("2. Check .git/hooks-controller.log for logs")
    print("3. Use tools/config.py for management")
    print("4. Try: git commit -m 'feat: your message'")

if __name__ == "__main__":
    main()

